﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
using System.Data;
using System.Configuration;

namespace KPIS.GERP.GHRMS.DAL
{
    public class TrainingScholarshipTypeDAL : ITrainingScholarshipTypeDAL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();

        public IList<TrainingScholarshipTypeInfo> GetFull()
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<TrainingScholarshipTypeInfo> infoList = new List<TrainingScholarshipTypeInfo>();

            try 
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT st.scholar_type_seq"
                    + ", st.scholar_type_descr"
                    + ", st.scholar_local"
                    + ", CASE WHEN st.scholar_local not like 0 THEN 'ในประเทศ' ELSE 'ต่างประเทศ' END AS local"
                    //+ ", CASE WHEN st.edu_time_type not like 0 THEN 'ลาศึกษา' ELSE 'นอกเวลาทำงาน' END AS edu_time_type"
                    //+ ", st.scholar_local"
                    //+ ", st.edu_time_type"                    
                    + " FROM  hrm_scholarship_types st"
                    + " WHERE st.record_status not like @delete_status";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        TrainingScholarshipTypeInfo info = new TrainingScholarshipTypeInfo();
                        info.scholar_type_seq = Convert.ToInt32(drc[i]["scholar_type_seq"]);
                        info.scholar_type_descr = drc[i]["scholar_type_descr"].ToString();
                        info.scholar_type_list = drc[i]["scholar_type_descr"].ToString() + " (" + drc[i]["local"].ToString()+")" ;
                        info.scholar_local = Convert.ToInt32(drc[i]["scholar_local"]);
                        info.local = drc[i]["local"].ToString();
                        //info.country_name = drc[i]["edu_time_type"].ToString();

                        infoList.Add(info);                       

                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<TrainingScholarshipTypeInfo> GetList(int scholar_type_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<TrainingScholarshipTypeInfo> infoList = new List<TrainingScholarshipTypeInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@scholar_type_seq", scholar_type_seq);

                strSQL = "SELECT st.scholar_type_seq"
                    + ", st.scholar_type_descr"
                    + " FROM  hrm_scholarship_types st"
                    + " WHERE st.record_status not like @delete_status"
                    + (scholar_type_seq > 0 ? " AND scholar_type_seq = @scholar_type_seq " : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        TrainingScholarshipTypeInfo info = new TrainingScholarshipTypeInfo();
                        info.scholar_type_seq = Convert.ToInt32(drc[i]["scholar_type_seq"]);
                        info.scholar_type_descr = drc[i]["scholar_type_descr"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public void Insert(TrainingScholarshipTypeInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@scholar_type_descr", info.scholar_type_descr);
                dbManager.AddParameters(1, "@scholar_local", info.scholar_local);
                dbManager.AddParameters(2, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(3, "@created_by", info.created_by);
                dbManager.AddParameters(4, "@created_when", info.created_when);

                strSQL = "INSERT INTO hrm_scholarship_types"
                    + "(scholar_type_descr, scholar_local, record_status"
                    + ", created_by, created_when)"
                    + " VALUES (@scholar_type_descr"
                    + ", @scholar_local"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when)";
                
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(TrainingScholarshipTypeInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@scholar_type_seq", info.scholar_type_seq);
                dbManager.AddParameters(1, "@scholar_type_descr", info.scholar_type_descr);
                dbManager.AddParameters(2, "@scholar_local", info.scholar_local);
                dbManager.AddParameters(3, "@updated_by", info.updated_by);
                dbManager.AddParameters(4, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_scholarship_types"
                    + " SET scholar_type_descr = @scholar_type_descr"
                    + ", scholar_local = @scholar_local"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE scholar_type_seq = @scholar_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(TrainingScholarshipTypeInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@scholar_type_seq", info.scholar_type_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE hrm_scholarship_types"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE scholar_type_seq = @scholar_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
